/**
 * 
 */
package pl.kkalisz.android.model.item;

import pl.kkalisz.android.R;
import pl.kkalisz.android.model.item.interfaces.uAction;
import pl.kkalisz.android.model.item.interfaces.uActionItemInterface;
import pl.kkalisz.android.model.item.interfaces.uBasicItemTypes;
import pl.kkalisz.android.model.item.interfaces.uOnClickedListner;
import pl.kkalisz.android.model.item.modules.uActionModule;
import pl.kkalisz.android.model.item.modules.uListenerModule;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

/**
 * @author kkalisz
 *
 */
public class uItemActionButton extends Button implements uActionItemInterface{

    uActionModule mActionModule = null;
    uListenerModule mListenerModuile = null;
    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public uItemActionButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
    
    public uItemActionButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    public uItemActionButton(Context context) {
        super(context);
        init();
    }
    
    public static uItemActionButton createFromLayout(Context mContext, int aResId)
    {
        LayoutInflater inflater = (LayoutInflater)   mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return (uItemActionButton) inflater.inflate(R.layout.item_action_button_standard, null);
    }
    
    public static uItemActionButton createFromLayout(Context mContext)
    {
        return createFromLayout(mContext, R.layout.item_action_button_standard);
    }
    
    public void init()
    {
        mActionModule = new uActionModule();
        mListenerModuile = new uListenerModule();
        this.setOnClickListener(new OnClickListener() {
            
            public void onClick(View arg0) {
                if(mActionModule!=null) mActionModule.invokeAction();
                if(mListenerModuile!=null) mListenerModuile.fire((uActionItemInterface)arg0);
                
            }
        });
        
        setText("ItemActionButton");
        
    }

    /* (non-Javadoc)
     * @see pl.kkalisz.android.ui.item.interfaces.BngActionItemInterface#setAction(pl.kkalisz.android.ui.item.interfaces.Action)
     */
    public void setAction(uAction aAction) {
       if(mActionModule != null) mActionModule.setAction(aAction);
        
    }

    /* (non-Javadoc)
     * @see pl.kkalisz.android.ui.item.interfaces.BngActionItemInterface#addClickedListner(pl.kkalisz.android.ui.item.interfaces.BngOnClickedListner)
     */
    public void addClickedListner(uOnClickedListner aListener) {
        if(mListenerModuile !=null ) mListenerModuile.addClickedListner(aListener);
        
    }

    /* (non-Javadoc)
     * @see pl.kkalisz.android.ui.item.interfaces.BngActionItemInterface#removerActionListener(pl.kkalisz.android.ui.item.interfaces.BngOnClickedListner)
     */
    public boolean removerActionListener(uOnClickedListner aListener) {
        if(mListenerModuile !=null ) return mListenerModuile.removerActionListener(aListener);
        return false;
    }

    /* (non-Javadoc)
     * @see pl.kkalisz.android.ui.item.interfaces.BngActionItemInterface#removeAllListeners()
     */
    public void removeAllListeners() {
        if(mListenerModuile !=null ) mListenerModuile.removeAllListeners();
        
    }

    /* (non-Javadoc)
     * @see pl.kkalisz.android.ui.item.interfaces.BngActionItemInterface#removeAction()
     */
    public void removeAction() {
        mActionModule.removeAction();
        
    }

    /* (non-Javadoc)
     * @see pl.kkalisz.android.ui.item.interfaces.BngItemInterface#getValue()
     */
    public String getValue() {
        return this.getText().toString();
    }

    /* (non-Javadoc)
     * @see pl.kkalisz.android.ui.item.interfaces.BngItemInterface#setValue(java.lang.String)
     */
    public void setValue(String aValue) {
        this.setText(aValue);
    }

    /* (non-Javadoc)
     * @see pl.kkalisz.android.ui.item.interfaces.BngItemInterface#getBasicItemType()
     */
    public uBasicItemTypes getBasicItemType() {
        return uBasicItemTypes.BUTTON;
    }

    
    

}
